//
//  CSRouteSelectCell.swift
//  Cycling
//
//  Created by CSS on 4/1/23.
//

import UIKit

import MapboxDirections
import MapboxCoreNavigation

class CSRouteSelectCell: UITableViewCell {
    
    @IBOutlet private weak var routeName: UILabel!
    @IBOutlet private weak var timeLbl: UILabel!
    @IBOutlet private weak var odometerLbl: UILabel!
    @IBOutlet private weak var startButton: UIButton!
    
    public var route: Route? {
        didSet {
            guard let route = route else {
                return
            }
            timeLbl.text = route.expectedTravelTime.formatRemainingString + "/ " + DistanceFormatter().string(from: route.distance)
            odometerLbl.text = DistanceFormatter().string(from: route.distance)
        }
    }
    public var routeIndex: Int = 0 {
        didSet {
            routeName.text = "线路\(routeIndex + 1):"
        }
    }
    
    public var goNavigationBlock:((Int) -> Void)?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setup()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        startButton.layoutIfNeeded()
        UIView.performWithoutAnimation { [weak self] in
            self?.startButton.layer.cornerRadius = startButton.frame.height / 2
        }
    }
    
    private func setup() {
        
        UIView.performWithoutAnimation { [weak self] in
            self?.startButton.layer.borderColor = UIColor.color(withHexString: "#0AD39F").cgColor
            self?.startButton.layer.borderWidth = 1
            self?.startButton.layer.cornerRadius = startButton.frame.height / 2
        }
        
        addSegmentLine()
        
        selectionStyle = .none
        backgroundColor = .white
    }
    
    @IBAction private func startButtonAction(_ sender: Any) {
        goNavigationBlock?(routeIndex)
    }
    
}
